
#include "QueryProjector.h"
#include "VarConstTable.h"
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

vector<string> QueryProjector::outputs;

void QueryProjector::arrangeResult(bool resultBoolean,vector<int> resultIndex,string resultType){
    // general idea of arrangeResult()
    // 1. the result is all in index form, so we need to access respected table to find the co-responding name
    // 2. sort the result in ascending format no matter they are integers or alphabets
	if ( resultBoolean ) {
		if ( resultIndex.empty() ) {
			outputs.push_back("false");
		}
		else {
			outputs.push_back("true");
		}
	}
	else if ( resultType=="s" ) {
		stringstream ss;
		for ( int i = 0; i < resultIndex.size(); ++i ) {
			ss.clear();
			ss << resultIndex.at(i);
			outputs.push_back( ss.str() );//convert int to string
		}
	}
	else if ( resultType=="v" ) {
		for ( int i = 0; i < resultIndex.at(i); ++i ) {
			outputs.push_back( VarConstTable::getName(resultIndex.at(i)) );
		}
	}
	else {
		cout<<"There was an error projecting your answer. We apologise for any inconvenience caused" <<endl;//ERROR NOTHING SHLD REACH HERE
	}
}

void QueryProjector::getResult(list<string>& results){//function for returning the result
	for ( int i = 0; i < outputs.size(); ++i ) {
		results.push_back( outputs.at(i) );
	}
}


